import string

# 定义列标签，跳过字母 'I'
columns = [chr(i) for i in range(ord('a'), ord('t') + 1) if chr(i) != 'i']

def print_board(board):
    # 打印列标签
    print("   ", end="")
    for col in columns:
        print(f"{col.lower()}", end=" ")
    print()
    
    for row in range(19):
        # 打印行号
        print(f"{row+1:2}", end=" ")
        for col in range(19):
            if board[row][col] == 0:
                print("+", end=" ")
            elif board[row][col] == 1:
                print("●", end=" ")
            elif board[row][col] == -1:
                print("○", end=" ")
        # 打印行号在右侧
        print(f"{row+1}", end="")
        print()
    
    # 打印底部列标签
    print("   ", end="")
    for col in columns:
        print(f"{col.lower()}", end=" ")
    print()

def get_move(player):
    while True:
        user_input = input(f"Player {player}, enter your move (e.g., a1): ").strip().lower()
        if len(user_input) < 2:
            print("Invalid input. Please try again.")
            continue
        col_label = user_input[0]
        row_label = user_input[1:]
        if col_label not in columns:
            print("Invalid column. Please try again.")
            continue
        try:
            row = int(row_label) - 1
            if row < 0 or row >= 19:
                print("Invalid row. Please try again.")
                continue
        except ValueError:
            print("Invalid row. Please try again.")
            continue
        col = columns.index(col_label)
        return row, col

def check_win(board, player):
    # 检查水平、垂直、对角线是否连成5子
    for row in range(19):
        for col in range(19):
            if (col + 4 < 19 and 
                all(board[row][col + i] == player for i in range(5))):
                return True
            if (row + 4 < 19 and 
                all(board[row + i][col] == player for i in range(5))):
                return True
            if (row + 4 < 19 and col + 4 < 19 and 
                all(board[row + i][col + i] == player for i in range(5))):
                return True
            if (row + 4 < 19 and col - 4 >= 0 and 
                all(board[row + i][col - i] == player for i in range(5))):
                return True
    return False

def main():
    board = [[0] * 19 for _ in range(19)]
    current_player = 1  # 1 for black, -1 for white
    print_board(board)
    
    while True:
        row, col = get_move(current_player)
        if board[row][col] != 0:
            print("This position is already occupied. Please choose another spot.")
            continue
        board[row][col] = current_player
        print_board(board)
        
        if check_win(board, current_player):
            print(f"Player {current_player} wins!")
            break
        
        current_player = -current_player  # 切换玩家

if __name__ == "__main__":
    main()